% ctex-common.def: common definitions for all ctex packages/classes
% vim:ft=tex

\def\CTEX@class{}
\@ifclassloaded{article}{\def\CTEX@class{article}}{}
\@ifclassloaded{report} {\def\CTEX@class{report}}{}
\@ifclassloaded{book}   {\def\CTEX@class{book}}{}

% Load some extra packages

\RequirePackage{ifpdf}
\RequirePackage{expl3}

\ExplSyntaxOn
\xetex_if_engine:TF
  { \CTEX@set@opt{encoding=UTF8,engine=xeCJK} }
  {
    \luatex_if_engine:T
      { \CTEX@set@opt{fntef=false,encoding=UTF8,engine=LuaCJK} }
  }
\cs_if_exist:NTF \str_if_eq_x:nnTF
  {
    \cs_new_eq:NN \CTEX@IfStrEq \str_if_eq_x:nnTF
    \cs_new_eq:NN \CTEX_str_if_eq_p_x:nn \str_if_eq_x_p:nn
  }
  {
    \cs_new_eq:NN \CTEX@IfStrEq \str_if_eq:xxTF
    \cs_new_eq:NN \CTEX_str_if_eq_p_x:nn \str_if_eq_p:xx
  }
\cs_new:Npn \ifCTEX@engines #1#2
  {
    \bool_if:nTF
      {
        \CTEX_str_if_eq_p_x:nn \CTEX@engine {#1} ||
        \CTEX_str_if_eq_p_x:nn \CTEX@engine {#2}
      }
  }
\cs_new_eq:NN \CTEX@trim@spaces \tl_trim_spaces:n
\ExplSyntaxOff

\RequirePackage{fix-cm}
\ifCTEX@fancyhdr
  \RequirePackage{fancyhdr}
\fi

% Useful definitions

\DeclareRobustCommand\CTeX{C\kern-.05em\TeX{}}
\newcommand*\CTEX@key{\define@key{CTEX}}
\newcommand*\CTEXoptions[1][]{\kvsetkeys{CTEX}{#1}}
\newcommand*\CTEX@subkey[1]{\define@key{CTEX#1}}
\newcommand*\CTEXsetup[2][]{\kvsetkeys{CTEX#2}{#1}}

\def\ifCTEX@encoding{\CTEX@IfStrEq\CTEX@encoding}
\def\ifCTEX@engine  {\CTEX@IfStrEq\CTEX@engine  }
\def\ifCTEX@fontset {\CTEX@IfStrEq\CTEX@fontset }
\def\ifCTEX@cls     {\CTEX@IfStrEq\CTEX@class   }

\def\CTEX@replacecommand#1#2#3{%
  \csletcs{#1#3}{#2#3}%
  \csdef{#2#3}{\csuse{#1#3}}}

% Select engine.

\ifCTEX@engine{xeCJK}
  {\input{ctex-xecjk-engine.def}}
  {\ifCTEX@engine{LuaCJK}
    {\input{ctex-luacjk-engine.def}}
    {\input{ctex-cjk-engine.def}}}
\input{ctex-cjk-common.def}

\ifCTEX@indent
  \RequirePackage{indentfirst}
\fi

\newcommand*\CTEXindent{\CTEXsetfont\parindent2\ccwd}
\newcommand*\CTEXnoindent{\parindent\z@}
\ifCTEX@indent
  \AtBeginDocument{\CTEXindent}
\fi
\def\baselinestretch{1.3}

% About numbers

\RequirePackage[encoding=\CTEX@encoding]{zhnumber}

\DeclareRobustCommand*\CTEXnumber[2]{\protected@edef#1{\zhnumber{#2}}}
\DeclareRobustCommand*\CTEXdigits[2]{\protected@edef#1{\zhdigits{#2}}}

\let\chinese\zhnum
\let\Chinese\chinese
\let\CTEXcounter\@gobble

% About caption

\ifCTEX@cap
  \let\CTEX@save@refstepcounter\refstepcounter
  \def\refstepcounter#1{\CTEX@save@refstepcounter{#1}%
    \protected@edef\@currentlabel
      {\csuse{p@#1}%
       \ifcsundef{CTEX@the#1}{\csuse{the#1}}{\csuse{CTEX@the#1}}%
      }}%
\fi

% `today' definitions

\CTEX@key{today}{%
  \ifcsundef{CTEX@settoday#1}
    {\PackageError{ctex}{%
       unknown today format}{%
       Available today format are "old", "small", and "big".}}
    {\csuse{CTEX@settoday#1}}}
\let\CTEX@todayold\today
\def\CTEX@settodayold{\let\today\CTEX@todayold}
\def\CTEX@settodaysmall{\let\today\zhtoday
  \zhnumsetup{time=Arabic}}
\def\CTEX@settodaybig{\let\today\zhtoday
  \zhnumsetup{time=Chinese}}
\ifCTEX@cap
  \CTEX@settodaysmall
\fi

% Put hyperref as bottom as possible, otherwise there may be page
% count issues
\ifCTEX@hyperref
\providecommand\hypersetup[1]{%
  \PassOptionsToPackage{#1}{hyperref}}
\AtEndPreamble{%
  \RequirePackage{hyperref}}
\AtEndOfPackage{%
  \ifCTEX@encoding{GBK}{%
    \hypersetup{CJKbookmarks}
    \unless\ifpdf % dvipdfmx
      \AtBeginDvi{\special{pdf:tounicode GBK-EUC-UCS2}}%
    \fi
  }{\hypersetup{unicode}}
  \hypersetup{driverfallback=dvipdfmx,colorlinks=true}}
\fi % hyperref

\endinput
